<!--划转-->
<template>
  <div class="all bg1" @rightBtn="rightBtn">
    
    <scroller class="all">
      <div class="wrapper" v-if="account">

        <!-- 账户 -->
        <div class="account bg2">

          <div class="account-row" @click="fromClick">
            <text class="font28 text2 width100">{{T('从')}}</text>
            <text class="font30 text2 all">{{fromAccount}}</text>
            <LImage class="img-arrow" :src="Config.img('v2_sanjiao_xia_bai.png')"></LImage>
          </div>

          <div class="row acenter">
            <div class="width100"></div>
            <div class="line all line1"></div>
          </div>


          <div class="account-row" @click="toClick">
            <text class="font28 text2 width100">{{T('到')}}</text>
            <text class="font30 text2 all">{{toAccount}}</text>
            <LImage class="img-arrow" v-if="toAccounts.length > 1" :src="Config.img('v2_sanjiao_xia_bai.png')"></LImage>
          </div>
          <LImage class="account-arrow" :src="Config.img('v2_sanjiao_xia_bai.png')"></LImage>
        </div>

        <!-- 币种 -->
        <div class="coin bg2" @click="coinClick">
          <text class="font30 text2 width100">{{T('币种')}}</text>
          <text class="font30 text2 all">{{hzCoin}}</text>
          <LImage class="img-arrow" v-if="account.coins.length > 1" :src="Config.img('xiala.png')"></LImage>
        </div>
        
        <!-- VTN权益 -->
        <div class="quity bg2" v-if="coin.switch != undefined">
          <div class="row acenter" @click="quityClick">
            <text class="font30 text2">{{T('含提现权益')}}</text>
            <LImage class="img-explain" :src="Config.img('v2_wenhao.png')"></LImage>
          </div>
          <div class="all"></div>
          <Switc class="switc" ref='switc' :isOn="coin.quity" :isEdit="coin.switch" @switcClick="switcClick"></Switc>
        </div>

        <!-- 输入 -->
        <div class="input bg2">
          <text class="font30 text2">{{T('数量') + '  '}}</text>
          <input class="input-text font28 text2" v-model="hzNumber" type="number" :placeholder="T('请输入划转数量')" @input="onInput" @focus="onFocus" @blur="onBlur">
          <div class="input-line line1"></div>
          <div class="input-all">
            <text class="font28 main-t1" @click="allClick">{{T('全部')}}</text>
          </div>
        </div>
        <div class="input-money">
          <text class="font24 text3">{{T('可用') + ' '+ coin.num + ' ' + hzCoin}}</text>
        </div>

        <!--提示-->
        <div class="tipe bg2">
          <text class="font30 main-t1 line-height52">{{T('提示')}}</text>
          <text class="font24 text3 line-height52">{{T('1、资产只有划转到相应账户时才可用于消费或使用平台各类产品及服务。')}}</text>
          <text class="font24 text3 line-height52">{{T('2、收益账户中的资产转出至其他账户后不可转回。')}}</text>
          <text class="font24 text3 line-height52">{{T('3、当存在保证金项目时，保证金不可进行划转操作。')}}</text>
        </div>
      </div>
    </scroller>

    <!--划转-->
    <div class="ok bg3">
      <div class="ok-but main-bt1" @click="okClick">
        <text class="font32 text2">{{T('划转')}}</text>
      </div>
    </div>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.scroller{
  flex: 1;
}
.wrapper{
  padding-left: 20px;
  padding-right: 20px;
}
.switc{
  width: 100px;
  height: 60px;
}
.line{
  height: 1;
}
.img-arrow{
  width: 22px;
  height: 13px;
  margin-left: 20px;
  margin-right: 30px;
}
.img-explain {
  width:30px;
  height:30px;
  margin-left: 10px;
}

/**************** 账户*/
.account {
  justify-content: center;
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 40px;

  margin-top: 30px;
  margin-bottom: 40px;
}
.account-row {
  flex-direction: row;
  align-items: center;
  height: 110px;
}
.account-arrow {
  position: absolute;
  width: 22px;
  height: 13px;
  margin-left: 4px;
}

/**************** 币种*/
.coin {
  flex-direction: row;
  align-items: center;
  height: 98px;
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 40px;

  margin-bottom: 40px;
}

/**************** 权限*/
.quity {
  flex-direction: row;
  align-items: center;
  height: 98px;
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 40px;

  margin-bottom: 40px;
}
/**************** 输入框*/
.input {
  flex-direction: row;
  align-items: center;
  height: 98px;
  border-radius: 24px;

  padding-left: 40px;

  margin-bottom: 20px;
}
.input-text {
  flex: 1;
  height: 98px;
  placeholder-color:#909AB4;
}
.input-line {
  width: 2px;
  height: 24px;
  margin-left: 20px;
}
.input-all {
  justify-content: center;
  align-items: center;
  height: 98px;
  padding-right: 40px;
  padding-left: 20px;
}
.input-money {
  padding-left: 40px;
  margin-bottom: 40px;
}

/*************** ok*/
.ok{
  flex-direction: column;
  height: 164px;

  padding-left: 40px;
  padding-right: 40px;
}
.ok-but{
  justify-content: center;
  align-items: center;
  height:72px;
  border-radius:12px;
  margin-top: 28px;
}

/**************** 提示*/
.tipe {
  border-radius: 24px;

  padding-left: 40px;
  padding-right: 40px;
  padding-top: 30px;
  padding-bottom: 30px;

  margin-bottom: 40px;
}
.line-height52 {
  line-height: 52px;
}

/**************** 间距*/
.width100{
  width: 100px;
}
.height98{
  height: 98px;
}
</style>


<script>

//功能
import jnavigator from "@/_js/navigator";
import jconfig from "@/_js/config";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jhttps from "@/_js/https";
import jdebug from "@/_js/debug";

const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;

weexFunction.setTitle(T('划转'));
weexFunction.setRightBtn(T('划转记录'));

export default {

  
  data() {
    return {
      T:T,
      Config:jconfig,

      wallet:undefined, //账户类型

      keyd:{}, //翻译对照表
      walletd:{'消费账户':'consumeWallet', 
              '收益账户':'incomeWallet', 
              '保证金账户':'depositWallet'},  //账户对照表

      fromAccount:"", //转出账户
      toAccount:"", //到账账户
      hzCoin:'', //划转币种

      hzNumber:'', ///划转数量
      
      accountd:undefined, //账户表
      fromAccounts:undefined, //"从"账户数组
      toAccounts:undefined, //“到”账户数组

      account:undefined, //账户信息
      coin:undefined, //币种信息
      accountMoney:undefined, //账户金额
    };
  },

  mounted() {
    
    this.initData();

    var param = weex.config;
    var fromAccount = param.fromAccount ? T(param.fromAccount) : this.fromAccounts[0];
    this.upFromAccount(fromAccount);

    this.httpTransferPage();
  },

  methods: {

    initData() {

      //账户数组，和对应翻译表
      var keyd = {};
      var fromAccounts = ['消费账户', '收益账户', '保证金账户'];
      for (let index = 0; index < fromAccounts.length; index++) {

        var name = fromAccounts[index];
        var tName = T(name);
        fromAccounts[index] = tName;
        keyd[tName] = name;
      }
      this.keyd = keyd;
      this.fromAccounts = fromAccounts;

      //划转表
      this.accountd = {
        '消费账户':{'toAccounts':[T('保证金账户')],

                  '保证金账户':{'coins':['VTN'],
                              'VTN':{'num':'0', 'switch':false, 'quity':true}}},
                              
              
        '收益账户':{'toAccounts':[T('消费账户'), T('保证金账户')],

                  '消费账户':{'coins':['VTN'],
                            'VTN':{'num':'0', 'switch':true, 'quity':true}},

                  '保证金账户':{'coins':['USDT', 'VT'],
                              'USDT':{'num':'0'},
                              'VT':{'num':'0'}}},


        '保证金账户':{'toAccounts':[T('消费账户')],

                    '消费账户':{'coins':['VTN'],
                              'VTN':{'num':'0', 'switch':false, 'quity':true}}}
        };
    },

    /*********************** 通信触发*/
    //右按钮触发
    rightBtn() {
      var nav = {'navColor':'#0c0c0c'};
      jnavigator.open("", "wallet/WalletHzRecord", undefined, nav);
    },

    /*********************** http*/
    //http划转首页
    httpTransferPage() {
      
      jmodal.showLoad();
      var key = this.keyd[this.fromAccount];
      var wallet = this.walletd[key];
      var param = {'wallet':wallet};
      jhttps.post("wallet/transferPage",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          data = data.data;
          this.accountMoney = data.incomeBalnace;
          this.upMoney();
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //http划转
    httpWalletZz() {
      
      jmodal.showLoad();
      var fromKey = this.keyd[this.fromAccount];
      var fromWallet = this.walletd[fromKey];

      var toKey = this.keyd[this.toAccount];
      var toWallet = this.walletd[toKey];

      var param = {'coinType': this.hzCoin, 
                   'amount':this.hzNumber, 
                   'isVtnEquity':this.coin.quity, 
                   'fromWallet':fromWallet, 
                   'toWallet':toWallet};
      
      jhttps.postX("wallet/walletTransfer",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          
          //减去划转币种
          var num = this.accountMoney[this.hzCoin];
          num = jmath.jian(num, this.hzNumber);
          this.accountMoney[this.hzCoin] = num;

          //减去划转提现权益
          if (this.coin.switch != undefined && this.coin.quity) {
            var equityCoin = this.hzCoin + '_equity';
            var equityNum = this.accountMoney[equityCoin];
            equityNum = jmath.jian(equityNum, this.hzNumber);
            this.accountMoney[equityCoin] = equityNum;
          }
          this.upMoney();
          jmodal.toast(T('划转成功'));
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    /*********************** 其他*/
    //转出账户改变
    upFromAccount(fromAccount) {

      var key = this.keyd[fromAccount];
      var map = this.accountd[key];

      //账户类型
      this.wallet = map.wallet;

      //转出账户
      this.fromAccount = fromAccount;

      //到账账户
      this.toAccount = map.toAccounts[0];
      
      //“到”账户数组
      this.toAccounts = map.toAccounts;

      //账户信息
      key = this.keyd[this.toAccount];
      this.account = map[key];

      //划转币种
      this.hzCoin = this.account.coins[0];

      //币种信息
      var coin = this.account[this.hzCoin];
      this.coin = JSON.parse(JSON.stringify(coin));
    },

    //到账账户改变
    upToAccount(toAccount) {

      var key = this.keyd[this.fromAccount];
      var map = this.accountd[key];
      
      //到账账户
      this.toAccount = toAccount;

      //账户信息
      key = this.keyd[this.toAccount];
      this.account = map[key];

       //划转币种
      this.hzCoin = this.account.coins[0];

      //'从'币种信息
      var coin = this.account[this.hzCoin];
      this.coin = JSON.parse(JSON.stringify(coin));
    },

    //划转币种改变
    upCoin(hzCoin) {

       //划转币种
      this.hzCoin = hzCoin;

      //'从'币种信息
      var coin = this.account[this.hzCoin];
      this.coin = JSON.parse(JSON.stringify(coin));
    },

    //更新可用-还原数据
    upMoney() {

      if (this.accountMoney == undefined) {
        return;
      }

      var num = '0';
      
      //不含提现权益
      if (this.coin.switch == undefined || !this.coin.quity) {
        num = this.accountMoney[this.hzCoin];
      } else { 
        var equityCoin = this.hzCoin + '_equity';
        var equityNum = this.accountMoney[equityCoin];
        num = this.accountMoney[this.hzCoin];
        num = parseFloat(equityNum) < num ? equityNum : num;
      }
      this.coin.num = num;

      this.hzNumber = '';
    },

    /*********************** 按钮*/
    //“从” 账户点击
    fromClick() {
      jmodal.actionSheet(this.fromAccounts, this.fromAccount, (value)=>{
        if (value == this.fromAccount) return;
        this.upFromAccount(value);
        this.httpTransferPage();
      });
    },

    //“到” 账户点击
    toClick() {
      if (this.toAccounts.length < 2) return;
      jmodal.actionSheet(this.toAccounts, this.toAccount, (value)=>{
        if(value == this.toAccount) return;
        this.upToAccount(value);
        this.upMoney();
      });
    },

    //币种点击
    coinClick() {
      
      if (this.account.coins.length < 2) return;
      jmodal.actionSheet(this.account.coins, this.hzCoin, (value)=>{
        if(value == this.hzCoin) return;
        this.upCoin(value);
        this.upMoney();
      });
    },

    //提现权益开关
    switcClick(e){
      this.coin.quity = e.isOn;
      this.upMoney();
    },

    //提现权益说明
    quityClick(){
      jmodal.alert(T('只有带提现权益的VTN才可以提现。'));
    },

    //全部
    allClick() {
      this.hzNumber = this.coin.num;
    },

    //划转
    okClick() {
      
      if (this.hzNumber.length == 0) {
        jmodal.toast(T('请输入划转数量'));
        return true;
      }

      if (this.hzNumber == 0) {
        jmodal.toast(T('划转数量不可为0'));
        return true;
      }

      if (parseFloat(this.hzNumber) > this.coin.num) {
        jmodal.toast(T('可用余额不足'));
        return true;
      }
      this.httpWalletZz();
    }
  }
}

</script>

